﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MIDIRemapper.DeviceInterfaces;

namespace MIDIRemapper
{
    static class TypeGetter
    {

        /// <summary>
        /// Gets a string representation of a Midi note.
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string GetStringNote(int i)
        {
            //60 = middle C
            string temp = string.Empty;
            switch (i % 12)
            {
                case 0: temp = "C";
                    break;
                case 1: temp = "C#";
                    break;
                case 2: temp = "D";
                    break;
                case 3: temp = "D#";
                    break;
                case 4: temp = "E";
                    break;
                case 5: temp = "F";
                    break;
                case 6: temp = "F#";
                    break;
                case 7: temp = "G";
                    break;
                case 8: temp = "G#";
                    break;
                case 9: temp = "A";
                    break;
                case 10: temp = "A#";
                    break;
                case 11: temp = "B";
                    break;
            }
            return (temp + (Math.Floor((double)i/12)));
        }

        /// <summary>
        /// Gets the number which the given string begins with. Returns
        /// -1 if string equals "All" or "Random".
        /// </summary>
        /// <param name="s">String beginning with a number or equal to "All" or "Random"</param>
        /// <returns>Parsed number or -1</returns>
        public static int ParseMidiNum(string s)
        {

            if (s == null || s.Length == 0)
            {
                throw new InvalidCastException("Cannot parse empty string.");
            }
            if (s == "All" || s == "Random" || s == "N/A") return -1;
            //else if (s == "N/A") return 0;
            //else if (s == "Random") return -2; //TODO: remove
            else
            {
                int i = 0;
                for (; i < s.Length && char.IsNumber(s[i]); i++) { }
                if (i == 0)
                {
                    throw new InvalidCastException("Could not parse string.");
                }
                return int.Parse(s.Substring(0, i));
            }
        }
    }
}
